package com.appkqxs.paser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GameLoad {

	/**
	 * @param args
	 */
	private String getFileSWF(String url) throws Exception {
		Document doc = Jsoup
				.connect(url)
				.userAgent(
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
				.get();
		Elements els = doc.select("div#maingamecontent");
		Elements elsScript = els.select("script");
		Element a = elsScript.get(6);
		String b = a.toString().replaceAll("\\s", "");
		String linkGame = b
				.substring(
						b.indexOf("varswf_location")
								+ "varswf_locatio=\"".length() + 1,
						b.lastIndexOf(";if(!active_user.getsInstartOptimizations())") - 1);
		return linkGame;

	}

	public String getDownloadFile(String urlFile) throws Exception {
		
		
		String file = urlFile.substring(urlFile.lastIndexOf("/")+1,urlFile.lastIndexOf(".swf")+4);
		System.out.println(file);
		SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
		String uploadImage = "C://"+dateFormat.format(new Date());

		File mkFloder = new File(uploadImage);
		if (!mkFloder.exists()) {
			mkFloder.mkdir();
		}

		URL url = new URL(urlFile);
		URLConnection urlConnection = url.openConnection();
		urlConnection
				.addRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
		InputStream inputStreamReader = urlConnection.getInputStream();
		FileOutputStream outputStreamWriter = new FileOutputStream(new File(
				uploadImage+ "//"+file));
		byte[] b = new byte[1024];
		int read;
		while ((read = inputStreamReader.read(b, 0, b.length)) != -1) {
			outputStreamWriter.write(b, 0, read);
		}
		outputStreamWriter.flush();
		outputStreamWriter.close();
		inputStreamReader.close();

		return uploadImage+ "\\"+file;
	}

	public void getListGame() throws Exception {

		Document doc = Jsoup
				.connect(
						"http://www.kongregate.com/action-games?pdis=qj-b&srid=15")
				.userAgent(
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
				.get();
		Elements els = doc
				.select("div.category_listing > div.category_games_listing.hasLayout  div.callout_listing.js-game-hover");
		els.remove("script");
		Elements elsText = els.select("div.game.media dl.thumb.img dt > a");
		for (Element element : elsText) {

			Elements elements = element.select("img");
			String alt = elements.attr("alt");
			String src = elements.attr("src");
			String href = element.attr("href");
			System.out.println(alt);
			System.out.println(src);
			System.out.println(href);
			try {
				System.out.println(getFileSWF(href));
			} catch (Exception e) {
				System.out.println("Khong lay duoc file bo qua");
			}
		}

	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new GameLoad().getListGame();

	}

}
